package com.mrjy.wechat.dto;

import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 发送的模板消息对象
 */
public class TemplateMessage {
	private String openid;//粉丝id
    private List<String> openids;//粉丝ids
    private String templateId;//模板id
	private String url;//链接
	private Map<String,String> miniprogram;//小程序
	private String appid;//小程序参数appId
	private String pagepath;//小程序跳转路劲
	private String color = "#173177";//颜色
	private Map<String,String> dataMap;//参数数据
	
	//根据模板标号获取一个模板消息,不做 toString
	private String templateIdShort;
	//365家长课堂业务消息类型，用于模板枚举获取模板ID，不做 toString
	private String sendType;
	
	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public String getTemplateId() {
		return templateId;
	}

	public void setTemplateId(String templateId) {
		this.templateId = templateId;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public Map<String, String> getMiniprogram() {
		return miniprogram;
	}

	public void setMiniprogram(Map<String, String> miniprogram) {
		this.miniprogram = miniprogram;
	}
	
	public String getOpenid() {
		return openid;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getPagepath() {
		return pagepath;
	}

	public void setPagepath(String pagepath) {
		this.pagepath = pagepath;
	}
	

	public Map<String, String> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, String> dataMap) {
		this.dataMap = dataMap;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}
	
	
	
	public String getTemplateIdShort() {
		return templateIdShort;
	}

	public void setTemplateIdShort(String templateIdShort) {
		this.templateIdShort = templateIdShort;
	}

	public String getSendType() {
		return sendType;
	}

	public void setSendType(String sendType) {
		this.sendType = sendType;
	}

	@Override
	public String toString(){
		JSONObject jsObj = new JSONObject();
        if (openid != null) {
            jsObj.put("touser", openid);
        } else {
            jsObj.put("touser", openids);
        }

		jsObj.put("template_id", templateId);
		jsObj.put("url", url);
		
		if(StringUtils.isNotBlank(appid) && StringUtils.isNotBlank(pagepath)){
			miniprogram.put("appid", appid);
			miniprogram.put("pagepath", pagepath);
			jsObj.put("miniprogram", miniprogram);
		}
		
		JSONObject data = new JSONObject();
		if(dataMap != null){
			for(String key : dataMap.keySet()){
				JSONObject item = new JSONObject();
				item.put("value", dataMap.get(key));
				item.put("color", color);
				data.put(key,item);
			}
		}
		jsObj.put("data", data);
		return jsObj.toString();
	}

    public List<String> getOpenids() {
        return openids;
    }

    public void setOpenids(List<String> openids) {
        this.openids = openids;
    }
}
